package com.wuercloud.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wuercloud.entity.ServiceEntity;
import com.wuercloud.service.ServiceService;
import com.wuercloud.utils.PageUtils;
import com.wuercloud.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 * 服务管理
 * 
 * @author ChinaSun
 * @email hjsw1@126.com
 * @date 2017-03-20 10:26:59
 */
@RestController
@RequestMapping("service")
@Api(value = "service", description = "服务管理")
public class ServiceController {
	@Autowired
	private ServiceService serviceService;

	/**
	 * 所有服务列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("service:list")
	@ApiOperation(value = "服务列表信息", notes = "返回服务列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public R list(@ApiParam(value = "服务名称", required = false) @RequestParam(required = false) String serviceName,
			@ApiParam(value = "第几页", required = true) @RequestParam(required = false) Integer page,
			@ApiParam(value = "每页显示几条", required = true) @RequestParam(required = false) Integer limit) {
		Map<String, Object> map = new HashMap<>();
		map.put("serviceName", serviceName);
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);

		// 查询列表数据
		List<ServiceEntity> serviceList = serviceService.queryList(map);
		int total = serviceService.queryTotal(map);

		PageUtils pageUtil = new PageUtils(serviceList, total, limit, page);

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 场景列表
	 */
	@GetMapping("/select")
	@RequiresPermissions("service:select")
	@ApiOperation(value = "服务列表信息(list)", notes = "返回服务列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public R select() {
		// 查询列表数据
		List<ServiceEntity> list = serviceService.queryList(new HashMap<String, Object>());

		return R.ok().put("list", list);
	}

	/**
	 * 信息
	 */
	@GetMapping("/info/{serviceId}")
	@RequiresPermissions("service:info")
	@ApiOperation(value = "根据ID查询服务信息", notes = "返回根据ID查询的服务信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public R info(@ApiParam(value="根据ID查询对象",required=true)@PathVariable("serviceId") Integer serviceId) {
		ServiceEntity service = serviceService.queryObject(serviceId);
		return R.ok().put("service", service);
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@RequiresPermissions("service:save")
	@ApiOperation(value = "保存服务信息", notes = "保存服务信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R save(@RequestBody ServiceEntity service) {
		serviceService.save(service);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@RequiresPermissions("service:update")
	@ApiOperation(value = "修改服务信息", notes = "修改服务信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R update(@RequestBody ServiceEntity service) {
		serviceService.update(service);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@RequiresPermissions("service:delete")
	@ApiOperation(value = "删除服务信息", notes = "删除服务信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public R delete(@RequestBody Integer[] serviceIds) {
		serviceService.deleteBatch(serviceIds);

		return R.ok();
	}

}
